﻿using Microsoft.AspNetCore.Mvc;
using Qy.Models;
using Qy.Service;
using System.Threading.Tasks;

namespace Qy.Web.Home
{
    [Area("Home")]
    public class InfoController : Base
    {
        private readonly ContentService _contentService;
        private readonly ContentCateService _contentCateService;
        private readonly AttmentService _attment;
        public InfoController(ContentService contentService, ContentCateService contentCateService, AttmentService attment)
        {
            _contentService = contentService;
            _contentCateService = contentCateService;
            _attment = attment;
        }

        /// <summary>
        /// 根据ID查询详细内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> IndexAsync(int id = 0)
        {
            if (id <= 0)
            {
                return Ok("请勿非法输入！");
            }
            ViewContentAndContent article = await _contentService.GetAsync(id);
            if (article == null)
            {
                return NotFound();
            }
            
            if (!string.IsNullOrEmpty(article.Attments))
            {
                ViewBag.attments =  await _attment.GetListAsync(article.Attments);
            }
            else
            {
                ViewBag.attments = null;
            }
                

            ContentCate cateInfo = await _contentCateService.GetOneAsync(article.Cid);
            ViewBag.info = article;
            ViewBag.cateInfo = cateInfo;
            return View();
        }

    }
}
